package com.park.service.impl.article;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.park.client.model.SearchText;
import com.park.client.model.article.ArticleModel;
import com.park.client.model.pub.ReptileUrlModel;
import com.park.client.service.article.ArticleService;
import com.park.client.vo.ArticleVo;
import com.park.common.GetArticle;
import com.park.common.empty.EmptyUtils;
import com.park.common.id.IdFactoryUtils;
import com.park.common.util.Result;
import com.park.dao.article.ArticleMapper;
import com.park.dao.pub.ReptileUrlMapper;
import com.park.service.impl.base.BaseServiceImpl;

@Service("articleService")
public class ArticleServiceImpl extends BaseServiceImpl< ArticleModel> implements ArticleService {
	private static final Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);
	@Autowired
	private ArticleMapper articleMapper;
	@Autowired
	private ReptileUrlMapper reptileUrlMapper;
	
	@Override
	public boolean insert(ArticleModel article) {
		return articleMapper.insert(article) == 1;
	}

	@Override
	public List<ArticleModel> selectRecommenRecord() {
		return articleMapper.selectRecommenRecord();
	}

	@Override
	public Integer updateClickRate(Long id) {
		return articleMapper.updateClickRate(id);
	}

	@Override
	public List<ArticleModel> listRelationUser(ArticleModel article) {
		return  articleMapper.listRelationUser(article);
	}

	@Override
	public Integer updateComment(Map<String,Object> map) {
		
		return articleMapper.updateComment(map);
	}

	@Override
	public boolean deleteByPrimaryKey(Long id) {
		return articleMapper.deleteByPrimaryKey(id)==1;
	}

	@Override
	public boolean insertSelective(ArticleModel record) {
		return articleMapper.insertSelective(record)==1;
	}

	@Override
	public ArticleModel selectByPrimaryKey(Long id) {
		return articleMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean updateByPrimaryKeySelective(ArticleModel record) {
		return articleMapper.updateByPrimaryKeySelective(record)==1;
	}

	@Override
	public boolean updateByPrimaryKey(ArticleModel record) {
		return articleMapper.updateByPrimaryKey(record)==1;
	}


	@Override
	public List<ArticleModel> selectAll() {
		return articleMapper.selectAll();
	}

	@Override
	public List<ArticleModel> selectByCategoryType(Long categoryId) {
		return articleMapper.selectByCategoryType(categoryId);
	}

	@Override
	public  List<ArticleVo> selectByType(ArticleVo articleVo) {
		return articleMapper.selectByType(articleVo);
	}

	@Override
	public Integer selectByTypeTotal(Long articleTypeId) {
		return articleMapper.selectByTypeTotal(articleTypeId);
	}

	@Override
	public boolean putToRecycle(Long id) {
		return articleMapper.putToRecycle(id)==1;
	}

	@Override
	public boolean setToDraft(Long id) {
		return articleMapper.setToDraft(id)==1;
	}

	@Override
	public boolean recoverFromRecycle(Long id) {
		return articleMapper.recoverFromRecycle(id)==1;
	}

	@Override
	public Integer selectByCategoryListTotal(Long categoryType) {
		return articleMapper.selectByCategoryListTotal(categoryType);
	}

	@Override
	public List<ArticleModel> selectOtherInterested(List<String> list) {
		return articleMapper.selectOtherInterested(list);
	}

	@Override
	public  Result<Boolean> updateReview(List<ArticleModel> records) {
		 Result<Boolean> result = new Result<>(true);
		 try {
			this.batchUpdateStatus(records);
			result.setMessage("审核成功");
		} catch (Exception e) {
			logger.error("文章审核出错",e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public List<ArticleVo> selectNewArticleComment() {
		return articleMapper.selectNewArticleComment();
	}

	@Override
	public List<ArticleModel> selectByStatus(ArticleModel t) {
		return articleMapper.selectByStatus(t);
	}

	@Override
	public Integer selectByStatusTotal(ArticleModel t) {
		return articleMapper.selectByStatusTotal(t);
	}

	@Override
	public List<ArticleVo> selectByArticleId(Long articleId) {
		return articleMapper.selectByArticleId(articleId);
	}
	
	@Override
	public List<ArticleVo> selectPagination(ArticleVo t) {
		return articleMapper.selectPagination(t);
	}

	@Override
	public Integer selectTotal(ArticleVo t) {
		return articleMapper.selectTotal(t);
	}

	@Override
	public List<ArticleVo> selectByCategoryList(ArticleVo articleVo) {
		return articleMapper.selectByCategoryList(articleVo);
	}

	@Override
	public List<ArticleModel> selectNewArticle() {
		return articleMapper.selectNewArticle();
	}

	@Override
	public List<ArticleModel> selectHotCommentArticle() {
		return articleMapper.selectHotCommentArticle();
	}

	@Override
	public ArticleModel selectPreArticle(ArticleModel record) {
		return articleMapper.selectPreArticle(record);
	}

	@Override
	public ArticleModel selectNextArticle(ArticleModel record) {
		return articleMapper.selectNextArticle(record);
	}
	
	@Override
	public void saveOrUpdate(ArticleModel record) {
		if(record.getId() == null){
			record.setId(IdFactoryUtils.getId());
			this.insert(record);
		}else{
			this.updateByPrimaryKey(record);
		}
	}

	@Override
	public void batchUpdateStatus(List<ArticleModel> list) {
		articleMapper.batchUpdateStatus(list);
	}

	@Override
	public void saveArticleFromCSDN() {
		List<ReptileUrlModel> reptileUrlList = reptileUrlMapper.selectAll();
		if(EmptyUtils.isEmpty(reptileUrlList)){
			return;
		}
		for (ReptileUrlModel reptileUrl : reptileUrlList) {
			List<ArticleModel> articlelist = GetArticle.getListFromCSDN(reptileUrl.getUrl());
			if(EmptyUtils.isEmpty(articlelist)){
				continue;
			}
			for (ArticleModel article : articlelist) {
				saveOrUpdate(article);
			}
		}
	}

	@Override
	public List<SearchText> querySearchList(SearchText searchText) {
		return this.articleMapper.querySearchList(searchText);
	}

	@Override
	public Integer querySearchListTotal(SearchText searchText) {
		return this.articleMapper.querySearchListTotal(searchText);
	}
}
